<?php
namespace Mobile\Controller;

use Common\Controller\UserBaseController;

/**
 * 首页控制器
 */
class AgentController extends UserBaseController
{
    /**
     * 首页
     */
    public function index()
    {
        $a = path_encode($_SESSION['member_id']);
        //print_r(path_decode($a));exit;
        $domain = 'http://' . $_SERVER['SERVER_NAME'] . '/index.php/Register/reg';
        $url    = $domain . '?p=' . $a;
        $info   = D('Member')->getOneMember($_SESSION['member_id']);
        if ($info['pan'] == 'C') {
            $name = 'dl_rebate_c';
        } else {
            $name = 'dl_rebate';
        }

        //上级返水参数
        if ($info['parent_id'] > 0) {
            $rebate             = M('member')->field('dl_rebate_c,dl_rebate')->where('member_id=' . $info['parent_id'])->find();
            $dl_rebate_parent   = unserialize($rebate['dl_rebate']);
            $dl_rebate_parent_c = unserialize($rebate['dl_rebate_c']);

        } else {
            $rebate = M('dl_rebate_setting')->where("pan='A'")->select();

            foreach ($rebate as $k => $v) {
                $dl_rebate_parent[$v['c_id']] = $v['percentage'];
            }
            //C盘
            $rebate_c = M('dl_rebate_setting')->where("pan='C'")->select();

            foreach ($rebate_c as $k => $v) {
                $dl_rebate_parent_c[$v['c_id']] = $v['percentage'];
            }

        }

        $rebate = M('member')->field('dl_rebate_c,dl_rebate')->where('member_id=' . $_SESSION['member_id'])->find();
        if (empty($rebate['dl_rebate'])) {
            foreach ($dl_rebate_parent as $k => $v) {
                $value = $v - 0.1;
                if ($value < 0) {
                    $value = 0;
                }
                $dl_rebate[$k] = $value;
            }
        } else {
            $dl_rebate = unserialize($rebate['dl_rebate']);
        }
        if (empty($rebate['dl_rebate_c'])) {
            foreach ($dl_rebate_parent_c as $k => $v) {
                $value = $v - 0.1;
                if ($value < 0) {
                    $value = 0;
                }
                $dl_rebate_c[$k] = $value;
            }
        } else {
            $dl_rebate_c = unserialize($rebate['dl_rebate_c']);
        }

        //print_r($dl_rebate_parent_c);exit;

        $this->assign('info', $info);
        $this->assign('url', $url);
        $this->assign('dl_rebate', $dl_rebate);
        $this->assign('dl_rebate_parent', $dl_rebate_parent);
        $this->assign('dl_rebate_c', $dl_rebate_c);
        $this->assign('dl_rebate_parent_c', $dl_rebate_parent_c);
        $this->display();
    }
    public function index_hk6()
    {
        $a = path_encode($_SESSION['member_id']);
        //print_r(path_decode($a));exit;
        $domain = 'http://' . $_SERVER['SERVER_NAME'] . '/index.php/Register/reg';
        $url    = $domain . '?p=' . $a;
        $info   = D('Member')->getOneMember($_SESSION['member_id']);
        if ($info['pan'] == 'C') {
            $name = 'dl_rebate_c_hk';
        } else {
            $name = 'dl_rebate_hk';
        }

        //上级返水参数
        if ($info['parent_id'] > 0) {
            $rebate             = M('member')->field('dl_rebate_c_hk,dl_rebate_hk')->where('member_id=' . $info['parent_id'])->find();
            $dl_rebate_parent   = unserialize($rebate['dl_rebate_hk']);
            $dl_rebate_parent_c = unserialize($rebate['dl_rebate_c_hk']);

        } else {
            $rebate = M('dl_rebate_setting')->where("c_id=12 and pan='A'")->select();

            foreach ($rebate as $k => $v) {
                $dl_rebate_parent[$v['c_type']] = $v['percentage'];
            }
            //C盘
            $rebate_c = M('dl_rebate_setting')->where("c_id=12 and pan='C'")->select();

            foreach ($rebate_c as $k => $v) {
                $dl_rebate_parent_c[$v['c_type']] = $v['percentage'];
            }

        }

        $rebate = M('member')->field('dl_rebate_c_hk,dl_rebate_hk')->where('member_id=' . $_SESSION['member_id'])->find();

        if (empty($rebate['dl_rebate_hk'])) {
            foreach ($dl_rebate_parent as $k => $v) {
                $value = $v - 0.1;
                if ($value < 0) {
                    $value = 0;
                }
                $dl_rebate[$k] = $value;
            }
        } else {
            $dl_rebate = unserialize($rebate['dl_rebate_hk']);
        }

        if (empty($rebate['dl_rebate_c_hk'])) {
            foreach ($dl_rebate_parent_c as $k => $v) {
                $value = $v - 0.1;
                if ($value < 0) {
                    $value = 0;
                }
                $dl_rebate_c[$k] = $value;
            }
        } else {
            $dl_rebate_c = unserialize($rebate['dl_rebate_c_hk']);
        }

        //print_r($dl_rebate_parent_c);exit;

        $this->assign('info', $info);
        $this->assign('url', $url);
        $this->assign('rebate', $dl_rebate);
        $this->assign('rebate_parent', $dl_rebate_parent);
        $this->assign('rebate_c', $dl_rebate_c);
        $this->assign('rebate_parent_c', $dl_rebate_parent_c);
        $this->display();
    }

    public function change_dl_rebate()
    {

        $pan = $_POST['pan'];
        if ($pan == 'C') {
            $name   = 'dl_rebate_c';
            $rebate = M('Member')->where('member_id=' . $_SESSION['member_id'])->getField('dl_rebate');
        } else {
            $name   = 'dl_rebate';
            $rebate = M('Member')->where('member_id=' . $_SESSION['member_id'])->getField('dl_rebate_c');
        }
        $rebate_hk = M('Member')->where('member_id=' . $_SESSION['member_id'])->getField('dl_rebate_c_hk');
        $dl_rebate = serialize($_POST[$name]);

        $update_array        = array();
        $update_array[$name] = $dl_rebate;
        if (!empty($rebate) && !empty($rebate_hk)) {
            $update_array['is_dl_save'] = 1;
        }

        $result = M('member')->where('member_id=' . $_SESSION['member_id'])->save($update_array);
        if ($result >= 0) {
            $this->success('保存成功', '/index.php/Member/index#/agent/index');exit;
        } else {
            $this->error('保存失败', '/index.php/Member/index#/agent/index');exit;
        }
    }
    public function change_dl_rebate_hk()
    {

        $rebate_a    = M('Member')->where('member_id=' . $_SESSION['member_id'])->getField('dl_rebate');
        $rebate_c    = M('Member')->where('member_id=' . $_SESSION['member_id'])->getField('dl_rebate_c');
        $dl_rebate   = serialize($_POST['ctype']);
        $dl_rebate_c = serialize($_POST['ctype_c']);

        $update_array                   = array();
        $update_array['dl_rebate_hk']   = $dl_rebate;
        $update_array['dl_rebate_c_hk'] = $dl_rebate_c;
        if (!empty($rebate_a) && !empty($rebate_c)) {
            $update_array['is_dl_save'] = 1;
        }

        $result = M('member')->where('member_id=' . $_SESSION['member_id'])->save($update_array);
        if ($result >= 0) {
            $this->success('保存成功', '/index.php/Member/index#/agent/index_hk');exit;
        } else {
            $this->error('保存失败', '/index.php/Member/index#/agent/index_hk');exit;
        }
    }

    public function child()
    {
        $info = D('Member')->getOneMember($_SESSION['member_id']);
        $this->assign('info', $info);
        $name  = $_GET['member_name'];
        $where = 'parent_id=' . $_SESSION['member_id'];
        if (!empty($name)) {
            $where .= " and member_name like '%" . $name . "%'";
        } else {
            $name = '';
        }

        $count = M('Member')->where($where)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出
        //$list=D('Member')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('member_id desc')->select();
        $list = D('Member')->where($where)->order('member_id desc')->select();
        $this->assign('list', $list);
        //$this->assign('page',$show);
        $this->assign('name', $name);
        $this->display();
    }

    public function dl_money()
    {
        $lottery                        = S('lottry_array');
        $prifix                         = C('DB_PREFIX');
        $map[$prifix . 'dl_fanli.m_id'] = $_SESSION['member_id'];
        $name                           = $_GET['member_name'];
        if (!empty($_GET['member_name'])) {
            $map[$prifix . 'member.member_name'] = array('like', '%' . $_GET['member_name'] . '%');
        } else {
            $name = '';
        }
        $begin = $_GET['begin'];
        $end   = $_GET['end'];
        if (empty($begin) || empty($end)) {
            $end           = time();
            $begin         = strtotime(date('Y-m-d', time())) - 7 * 86400;
            $_GET['begin'] = date('Y-m-d', $begin);
            $_GET['end']   = date('Y-m-d', $end);
        } else {
            $begin = strtotime($begin);
            $end   = strtotime($end) + 86400;
        }
        $map[$prifix . 'dl_fanli.add_time'] = array('between', array($begin, $end));

        $count = M('dl_fanli')->join('__MEMBER__ ON __DL_FANLI__.m_id = __MEMBER__.member_id', 'left')->where($map)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出

        $list    = M('dl_fanli')->field($prifix . 'member.member_name,' . $prifix . 'dl_fanli.add_time,' . $prifix . 'dl_fanli.c_id,' . $prifix . 'dl_fanli.money,' . $prifix . 'dl_fanli.f_money')->join('__MEMBER__ ON __DL_FANLI__.x_id = __MEMBER__.member_id', 'left')->where($map)->select();
        $money   = 0.00;
        $f_money = 0.00;
        foreach ($list as $k => $v) {
            $list[$k]['c_name'] = $lottery[$v['c_id']]['text'];
            $money += $v['money'];
            $f_money += $v['f_money'];
        }
        $info = D('Member')->getOneMember($_SESSION['member_id']);
        $this->assign('info', $info);

        $this->assign('money', $money);
        $this->assign('f_money', $f_money);
        $this->assign('list', $list);
        //$this->assign('page',$show);
        $this->assign('name', $name);
        $this->assign('begin', $_GET['begin']);
        $this->assign('end', $_GET['end']);
        $this->display();
    }
}
